CREATE OR REPLACE PACKAGE BODY ZOECOLLECT.ZOEPKG_COLLECT_PROGRAM
AS
-- 北极熊创建于： 2019.03.15
-- 版权 2019 中国
-- 保留所有权利
--	文件名:
-- 		文件名
--	描述:
-- 		基本说明
--   对象关联:
--      对象关联
--	注意事项:
--		基本注意事项
--	修改 - （年-月-日） - 描述
--
    FUNCTION I_GET_TABLE_COLUMN(iv_owner IN VARCHAR2, iv_table_name IN VARCHAR2)
        RETURN GTV_TABLE_COLUMN
    AS 
        ltv_table_column GTV_TABLE_COLUMN;
        ln_column_id NUMBER :=1;
    BEGIN
        FOR c1 IN (SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE OWNER = iv_owner AND TABLE_NAME = iv_table_name ORDER BY COLUMN_ID)
        LOOP
            ltv_table_column(ln_column_id) := c1.COLUMN_NAME;
            ln_column_id := ln_column_id + 1;
        END LOOP;
        RETURN ltv_table_column;
    END;
    
    FUNCTION I_COLLECT_RPOGRAM_SQL(iv_owner IN VARCHAR2, iv_table_name IN VARCHAR2, iv_collect_program IN VARCHAR2)
        RETURN NUMBER
    AS
        ltv_table_column GTV_TABLE_COLUMN;
        lv_columns      VARCHAR2(4000);
        lv_collect_sql  VARCHAR2(4000 CHAR);
    BEGIN
        ltv_table_column := I_GET_TABLE_COLUMN(iv_owner, iv_table_name);
        lv_columns := ltv_table_column(2);
        FOR i IN 3..ltv_table_column.COUNT
        LOOP
            lv_columns := lv_columns||','||ltv_table_column(i);
        END LOOP;
        lv_collect_sql := 'INSERT INTO '||iv_owner||'.'||iv_table_name;
        lv_collect_sql := lv_collect_sql||' ('||lv_columns||')';
        lv_collect_sql := lv_collect_sql||' '||iv_collect_program;
        EXECUTE IMMEDIATE lv_collect_sql;
        COMMIT;
        RETURN 0;
    END ;
    
    FUNCTION I_COLLECT_RPOGRAM_STORE_PROCEDURE(iv_collect_program IN VARCHAR2)
        RETURN NUMBER
    AS
        
    BEGIN
        --  待实现
        RETURN 0;
    END ;
    
    FUNCTION I_COLLECT_RPOGRAM(iv_collect_config IN VARCHAR2)
        RETURN NUMBER
    AS
        ln_return       NUMBER;
        -- 错误信息参数
        lv_error_program VARCHAR2(128) := SYS_CONTEXT('ZOE_DEVOPS_CONTEXT','CURRENT_PROGRAM');
        lv_sqlerrm      VARCHAR2(512);
        lv_error_date   VARCHAR2(64);
        lv_error_info_json    VARCHAR2(4000);
        --  采集程序通用配置参数
        lv_program_type VARCHAR2(64);
        lv_collect_parameter   VARCHAR2(4000);
        -- SQL采集程序配置参数
        lv_owner        VARCHAR2(64);
        lv_table_name   VARCHAR2(64);
    BEGIN
        --  设置程序名上下文  
        ZOEDEVOPS.ZOEPRC_SET_DEVOPS_CONTEXT('CURRENT_PROGRAM','ZOECOLLECT.ZOEPKG_COLLECT_PROGRAM.I_COLLECT_RPOGRAM');  
        SELECT JSON_VALUE(iv_collect_config,'$.COLLECT_PROGRAM_TYPE'),
            JSON_VALUE(iv_collect_config,'$.COLLECT_PARAMETER'),
            JSON_VALUE(iv_collect_config,'$.OWNER'),
            JSON_VALUE(iv_collect_config,'$.TABLE_NAME')
            INTO lv_program_type,lv_collect_parameter,lv_owner,lv_table_name
        FROM DUAL;
        IF lv_program_type = 'SQL' THEN
            ln_return := I_COLLECT_RPOGRAM_SQL(lv_owner,lv_table_name,lv_collect_parameter);
        ELSIF lv_program_type = 'STORE' THEN
            ln_return := I_COLLECT_RPOGRAM_STORE_PROCEDURE(lv_collect_parameter);
        END IF;
        RETURN 0;
    EXCEPTION 
        WHEN OTHERS THEN
            lv_sqlerrm := SQLERRM;
            lv_error_date := TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS');
            lv_error_info_json :=  ZOEDEVOPS.ZOEFUN_WERROR_JSON('PLSQLE0F'||lv_error_program||'E0F'||lv_sqlerrm||'E0F'||lv_collect_parameter||'E0F'||lv_error_date);
            ln_return := ZOEDEVOPS.ZOEFUN_WERROR_LOG(lv_error_info_json); 
            RETURN -1;
    END ;
    
	PROCEDURE COLLECT_PROGRAM(iv_object_id IN VARCHAR2, iv_schedule_id IN VARCHAR2)
	AS
        ln_return  NUMBER;
        ln_metric_id        NUMBER;
        lv_metric_name      VARCHAR2(400);
        lv_collect_config   VARCHAR2(4000 CHAR);
        lv_collect_enabled   VARCHAR2(3);
	BEGIN 
        FOR c1 IN (SELECT t.METRIC_ID,t.METRIC_NAME,t.COLLECT_CONFIG
            FROM ZOECOLLECT.CLC_METRIC_DICT t 
            WHERE t.METRIC_ID IN (SELECT METRIC_ID FROM CLC_OBJECT_METRIC_CONFIG WHERE OBJECT_ID=iv_object_id AND SCHEDULE_ID=iv_schedule_id))
        LOOP
            ln_metric_id        :=  c1.METRIC_ID;
            lv_metric_name      :=  c1.METRIC_NAME;
            lv_collect_config   :=  c1.COLLECT_CONFIG;
            SELECT JSON_VALUE(lv_collect_config,'$.ENABLED') INTO lv_collect_enabled from dual;
            --  判断指标是否启用，启用则执行采集程序I_COLLECT_RPOGRAM，传入参数采集配置
            IF lv_collect_enabled = 'YES' THEN
                 ln_return := I_COLLECT_RPOGRAM(lv_collect_config);
            END IF;
        END LOOP;
	END COLLECT_PROGRAM;

    PROCEDURE TEST_METRIC(in_metric_id IN NUMBER)
    AS
        ln_return  NUMBER;
        ln_metric_id        NUMBER;
        lv_metric_name      VARCHAR2(400);
        lv_collect_config   VARCHAR2(4000 CHAR);
        lv_collect_enabled   VARCHAR2(3);
	BEGIN 
            SELECT t.METRIC_ID,t.METRIC_NAME,t.COLLECT_CONFIG
            INTO ln_metric_id, lv_metric_name, lv_collect_config
            FROM ZOECOLLECT.CLC_METRIC_DICT t 
            WHERE t.METRIC_ID = in_metric_id;
            SELECT JSON_VALUE(lv_collect_config,'$.ENABLED') INTO lv_collect_enabled from dual;
            --  判断指标是否启用，启用则执行采集程序I_COLLECT_RPOGRAM，传入参数采集配置
            IF lv_collect_enabled = 'YES' THEN
                 ln_return := I_COLLECT_RPOGRAM(lv_collect_config);
            END IF;
   END TEST_METRIC;
   

   
END ZOEPKG_COLLECT_PROGRAM;